﻿using System.Collections.Generic;
using System.Threading.Tasks;
using Red.Core.Containers;

namespace Red.Controls.ViewModels
{
    public class ObjectsPaletteCategory : DataItem
    {
        public List<ObjectsPaletteItem> Items { get; set; }
        public List<ObjectsPaletteCategory> Subcategories { get; set; }

        public override IEnumerable<RedMenuDataItem> Menu
        {
            get
            {
                List< RedMenuDataItem > children = new List<RedMenuDataItem>();
                foreach ( var category in Subcategories )
                {
                    var item = new RedMenuDataItem( null, category.Title, category.Menu );
                    children.Add( item );
                }

                foreach ( var item in Items )
                    children.AddRange( item.Menu );

                return children;
            }
        }

        public override string Type
        {
            get { return ""; }
        }

        public override string Value { get; set; }

        public override DataItemKind Kind
        {
            get { return DataItemKind.Container; }
        }

        public ObjectsPaletteCategory( string name, DataItem parent )
            : base( parent )
        {
            Title = name;
            Subcategories = new List<ObjectsPaletteCategory>();
            Items = new List<ObjectsPaletteItem>();
        }

        protected override Task EnumerateChildrenAsync( IRangedCollection<DataItem> children )
        {
            if ( children.Count != 0 )
                return Task.CompletedTask;

            children.AddRange( Subcategories );
            children.AddRange( Items );

            return Task.CompletedTask;
        }
    }
}
